#!/usr/bin/perl
open(ANSWER_FILE,"<$ARGV[0]");
@ANSWER_LINES=<ANSWER_FILE>;
close(ANSWER_FILE);

open(GIZA_FILE,"<$ARGV[1]");
@GIZA_LINES=<GIZA_FILE>;
close(GIZA_FILE);

$COUNT=0;

$SUM_GIZA=0;
$SUM_ANSWER=0;
$SUM_CORRECT=0;

foreach $GIZA_LINE(@GIZA_LINES)
{
	$ANSWER_LINE=$ANSWER_LINES[$COUNT];
	$POS_LINE=$POS_LINES[$COUNT];
	$COUNT++;
	if($COUNT==1001)
	{
		last;
	}
	@ANSWER_ALIGNS=split('\s',$ANSWER_LINE);
	@GIZA_ALIGNS=split('\s',$GIZA_LINE);

	$N_ANSWER=0;
	$N_GIZA=0;
	$CORRECT=0;

	foreach $ANSWER_ALIGN(@ANSWER_ALIGNS)
	{
		$N_ANSWER++;
	}
	foreach $GIZA_ALIGN(@GIZA_ALIGNS)
	{
		$N_GIZA++;
	}
	foreach $ANSWER_ALIGN(@ANSWER_ALIGNS)
	{
		foreach $GIZA_ALIGN(@GIZA_ALIGNS)
		{
			if($ANSWER_ALIGN eq $GIZA_ALIGN)
			{
				$CORRECT++;
			}
		}
	}

	#modified by swlee
	$PREC = 1;
	if( $N_GIZA != 0 )	{
		$PREC = $CORRECT/$N_GIZA;
	}
	$RECL = 0;
	if( $N_ANSWER != 0 )	{
		$RECL = $CORRECT/$N_ANSWER;
	}
	$F = 0;
	if( $PREC+$RECL != 0 )	{
		$F = 2*$PREC*$RECL/($PREC+$RECL);
	}

	print "P: ", $PREC, " (", $CORRECT, "/", $N_GIZA, ")\t";
	print "R: ", $RECL, " (", $CORRECT, "/", $N_ANSWER, ")\t";
	print "F: ", $F, "\n";


	$SUM_GIZA+=$N_GIZA;
	$SUM_ANSWER+=$N_ANSWER;
	$SUM_CORRECT+=$CORRECT;
}

$TOTAL_PREC = 1;
if( $SUM_GIZA != 0 )	{
	$TOTAL_PREC = $SUM_CORRECT/$SUM_GIZA;
}
$TOTAL_RECL = 0;
if( $SUM_ANSWER != 0 )	{
	$TOTAL_RECL = $SUM_CORRECT/$SUM_ANSWER;
}
$TOTAL_F = 0;
if( $TOTAL_PREC+$TOTAL_RECL != 0 )	{
	$TOTAL_F = 2*$TOTAL_PREC*$TOTAL_RECL / ($TOTAL_PREC+$TOTAL_RECL);
}



print "\n";
print "All Average Precision : ",$TOTAL_PREC,"\n";
print "All Average Recall : ",$TOTAL_RECL,"\n";
print "All Average F-Measure : ",$TOTAL_F, "\n";
